package com.realtyreg.domain.entity.message;

import org.hibernate.annotations.Type;
import org.hibernate.validator.constraints.NotBlank;
import org.joda.time.DateTime;
import org.springframework.data.domain.Persistable;
import com.realtyreg.domain.entity.users.User;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * Сообщение от пользователя пользователю
 * Created by Petro 03.03.13 21:11
 */
@Entity
@Table(name = "message")
public class Message implements Persistable<Long> {
    private Long id; //id сообщения
    private User sender; //отправитель
    @NotNull
    private User receiver; //получатель
    private DateTime sendDate; //дата отправки
    private String subject; //тема
    @NotBlank
    private String body; //тело
    private boolean isRead = false; //прочитано?
    private boolean isSenderDel = false; //удалено отправителем?
    private boolean isReceiverDel = false; //удалено получателем?

    public Message() {
    }

    @Override
    public String toString() {
        return "Message{" +
                "id=" + id +
                ", senderId=" + sender.getId() +
                ", receiverId=" + receiver.getId() +
                '}';
    }

    @Transient
    public boolean isNew() {
        return id == null;
    }

    @Id
    @GeneratedValue
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    public User getSender() {
        return sender;
    }

    public void setSender(User sender) {
        this.sender = sender;
    }

    @ManyToOne(cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
    public User getReceiver() {
        return receiver;
    }

    public void setReceiver(User receiver) {
        this.receiver = receiver;
    }

    @Column(name = "send_date")
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentDateTime")
    public DateTime getSendDate() {
        return sendDate;
    }

    public void setSendDate(DateTime sendDate) {
        this.sendDate = sendDate;
    }

    @Column
    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    @Column
    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Column(name = "is_read")
    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    @Column(name = "is_sender_del")
    public boolean isSenderDel() {
        return isSenderDel;
    }

    public void setSenderDel(boolean senderDel) {
        isSenderDel = senderDel;
    }

    @Column(name = "is_receiver_del")
    public boolean isReceiverDel() {
        return isReceiverDel;
    }

    public void setReceiverDel(boolean receiverDel) {
        isReceiverDel = receiverDel;
    }
}
